package servlet;

import news.util.BaseHttpServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author Gong
 * @Description
 * @dATE 2022/3/2 14:56
 **/
@WebServlet("/AjaxServlet")
public class AjaxServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String email = req.getParameter("email");
        //2.查询数据库中是否存在email
        //select count(1) from users where email = ? ; >0说已被注册，可以使用

        // 设置响应内容类型
        resp.setContentType("text/html;charset=UTF-8");
//        JSON 文本的 MIME 类型是 "application/json"


        PrintWriter out = resp.getWriter();
        int emailcount = 1;
        if(emailcount > 0){
            //返回ajax  ，不可用（已注册）
            String res = "{" +
                    "            \"status\":200," +
                    "            \"msg\":\"fail\"" +
                    "        }";
            out.write(res);
        }else{
            //返回ajax  ，可用（未注册）
            String res = "{" +
                    "            \"status\":200," +
                    "            \"msg\":\"success\"" +
                    "        }";
            out.write(res);

        }
        out.flush();
        out.close();
    }
}
